{% extends 'frontend/common/basic_layout.html' %}

{% block content %}
    <div class="wrapper wrapper-content animated fadeInRight">
        <div class="row">
            <div class="col-sm-12">
                <div class="ibox float-e-margins">
                    <div class="ibox-title">
                        <h5>{{ title }}
                        </h5>
                    </div>
                    <div class="ibox-content">
                        <form method="post" action="{{ action }}" class="form-horizontal">
                            {% csrf_token %}
                            <input type="hidden" id="id" name="id" value="{{ pk }}"/>
                            {% for field in form %}
                                <div class="form-group">
                                    <label class="col-sm-2 control-label"> {{ field.label_tag }}</label>
                                    <div class="col-sm-10">
                                        {{ field }}
                                        {{ field.errors }}
                                    </div>
                                </div>
                            {% endfor %}

                            <div class="hr-line-dashed"></div>
                            <div class="form-group">
                                <div class="col-sm-4 col-sm-offset-2">
                                    <button class="btn btn-primary" type="submit">保存</button>
                                    <a class="btn btn-white" href="javascript:history.go(-1)">取消</a>
                                </div>
                            </div>
                        </form>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <script>
        {% ifequal is_add True%}
            $('#id_cabinet').empty();
            $("#id_cabinet").append("<option selected='selected' value=''>请选择机房</option>");
        {% endifequal %}
        {% ifequal is_add False%}
            $('#id_cabinet').empty();
            {% for cabinet in cabinet_list %}
                {% ifequal cabinet obj.cabinet.id %}
                    $("#id_cabinet").append("<option selected='selected' value='{{ cabinet.id }}'>{{ cabinet.name }}</option>");
                {% endifequal %}
                {% ifnotequal  cabinet obj.cabinet.id  %}
                    $("#id_cabinet").append("<option value='{{ cabinet.id }}'>{{ cabinet.name }}</option>");
                {% endifnotequal %}

            {% endfor %}
        {% endifequal %}
        $('#id_idc').change(function () {
            if ($('#id_idc').val() != '') {
                $.get('/frontend/cmdb/rack_list/' + $('#id_idc').val() + '/load_cabinet_list/', function (rs) {
                    optionList = eval(rs)
                    $('#id_cabinet').empty()
                    for (var i = 0; i < optionList.length; i++) {
                        $("#id_cabinet").append("<option value=" + optionList[i].pk + ">" + optionList[i].fields.name + "</option>");
                    }
                })
            } else {
                $('#id_cabinet').empty()
                $("#id_cabinet").append("<option value=''>请选择机房</option>");
            }
        })
    </script>
{% endblock %}